<template>
	<view class="Index">
		<view class="swiper">
			<uni-swiper-dot :info="bannerList" :current="current" :mode="mode">
				<swiper :autoplay="true" class="swiper-box" @change="change" style="height: 360rpx;width: 750rpx;">
					<swiper-item v-for="(item ,index) in bannerList" :key="index">
						<view class="swiper-item" style="background-color: #F8F8F8;">
							<image style="height: 360rpx;width: 100%;" :src="item.bannerfile.url" mode="aspectFit"></image>
						</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<!-- <button @click="getCode">获取小程序码</button> -->
		<view class="" style="padding: 20rpx 0;">
			<uni-easyinput v-model="checkcontent" placeholder="输入文本检测是否违禁"
			@confirm='checkeText'></uni-easyinput>
		</view>
		<!-- <button @click="checkeText">检测文本</button> -->
		<!-- #endif -->
		<view class="">
			<input class="uni-input" placeholder="输入内容" v-model='inputContent'
			style="height: 100rpx;"
			maxlength='-1'/>
			<button @click="sendContent">发送</button>
			<rich-text :nodes="nodes"></rich-text>
		</view>
		<!-- 上传轮播图 -->
		<!-- <view class="">
			<uni-file-picker v-model="imageValue" limit="1" file-mediatype="image" 
				:image-styles="imageStyles"
				return-type="array"
				:auto-upload="false"
				ref='files'
				mode="grid"
			    @select="select" 
			    @progress="progress" 
			    @success="success" 
				@delete="deletes"
			    @fail="fail">
			</uni-file-picker>
			<button @click="upload">上传文件</button>
			<button type="default" @click="getimage">获取文件信息</button>
			<button type="default" @click="deletes">删除</button>
		</view> -->
		<!-- <button open-type="getUserInfo" @getuserinfo="getuserinfo">获取手机号</button> -->
		<!-- <button type="default" @click="getaccess">获取access_token</button> -->
		<!-- <button type="default" @click="idcard">身份证校验</button> -->
		
		<!-- <button type="default" @click="imgSecCheck">上传图片鉴黄</button> -->
		<!-- <button @click="weatherEmail" type="primary">天气邮件的推送</button>
		<button @click="createSeal" type="primary">生成电子印章</button>
		<canvas canvas-id="seal" id='seal'></canvas> -->
		<button @click="enterCanvas">canvas</button>
		<view class="" style="width: 100%;">
			<view style="padding:20rpx 30rpx;">
				<uni-segmented-control :current="currents" :values="items" @clickItem="onClickItem" styleType="button" activeColor="#8f8f8f"></uni-segmented-control>
				<view class="content" >
					<view v-show="currents === 0">
						<uni-list v-for="(item,index) in newsHotList" :key='item._id'>
							<uni-list-item :title="item.title" :note="item.excerpt" :thumb="item.avatar?item.avatar:'/static/image/QQ.png'" clickable link @click="enterNews(item._id)"></uni-list-item>
						</uni-list>
					</view>
					<view v-show="currents === 1">
						<uni-list v-for="(item,index) in openComments" :key='item._id'>
							<uni-list-item :title="item.title" :note="item.excerpt" :thumb="item.avatar?item.avatar:'/static/image/QQ.png'" clickable link @click="enterNews(item._id)"></uni-list-item>
						</uni-list>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {time} from '@/tools/moment.js'
	import moment from 'moment'
	// moment.locale('zh-cn')
	import {mapGetters,mapMutations} from 'vuex'
	const db = uniCloud.database()
	const miniupload = uniCloud.importObject('mini-upload');
	// import { Official } from '@vsirrr/seal'
	import Seal from "y-seal";
	const chatGpt = uniCloud.importObject('chatgpt')
	export default {
		data() {
			return {
				nodes:'',
				inputContent:'',
				bannerList: [],
				current: 0,
				mode: 'default',
				imageStyles:{
					"height": 150,   // 边框高度
					"width": 600 ,   // 边框宽度
					"border":{ // 如果为 Boolean 值，可以控制边框显示与否
						"color":"#000",     // 边框颜色
						"width":"1px",      // 边框宽度
						"style":"solid",    // 边框样式
					}
				},
				imageValue:[],
				items:['热门','开放评论'],
				currents:0,
				newsHotList:[],//新闻热门排序
				openComments:[],//开放评论的新闻
				checkcontent:''
			}
		},
		computed:{
			...mapGetters({
				getInfo:'user/info'
			})
		},
		onLoad() {
			this.getbanner(),
			this.getNews()
		},
		onReady() {
			
		},
		methods: {
			async sendContent(){
				console.log(this.inputContent)
				try{
					const res = await chatGpt.getData(this.inputContent)
					console.log(res,res.response.data,res?.response?.data?.choices[0].text)
					this.nodes=res?.response?.data?.choices[0].text
				}catch(e){
					console.log(e)
				}
			},
			enterCanvas(){
				uni.navigateTo({
					url:'/pages/handleCanvas/handleCanvas'
				})
			},
			// 天气邮件的推送
			async weatherEmail(){
				let data=await miniupload.test()
				console.log(data)
			},
			createSeal(){
				// 方式1
				// const company =new Official('#seal',{
				// 	name: '广州诺曼数字化医疗科技有限公司',
				// 	// 类型(可选)：1.公司公章 2.合同专用章
				// 	type: '公司公章',
				// 	// 安全码(可选)
				// 	code: '1102311231231',
				// 	// 是否显示内边线，默认为 false
				// 	innerLine: true
				// })
				// company.saveSeal()
				
				// 方式2
				var seal = new Seal('#seal', {
					radius: 75,// 默认半径75px
					color: "red",// 默认颜色红色
					companyName: " 广州诺曼数字化医疗科技有限公司", //
					fontFamily: "宋体", // 默认宋体
					typeName: "公司公章", // 印章类型 默认为空
					hasInnerLine: true ,// 内边框 默认false
					securityCode: "1234567890123", // 防伪码 只能为13位 默认空
				}) 
				seal.saveSealImg(); // 保存成图片
			},
			// 获取小程序码
			async getCode(){
				let data=await miniupload.getminicode()
				console.log(data)
			},
			// 检查文本安全
			async checkeText(){
				// console.log(this.checkcontent)
				let res=await miniupload.checkeText({content:this.checkcontent})
				console.log(res)
				uni.showToast({
					title:res.data.result.suggest,
					icon:'none',
					duration:3000
				})
			},
			onClickItem(e){
				console.log(e)
				this.currents=e.currentIndex
			},
			// 获取新闻数据
			async getNews(){
				// 获取浏览量最靠前的数据
				let data=await db.collection('opendb-news-articles').where('article_status==1').orderBy("view_count,desc").limit(5).get()
				console.log(data)
				this.newsHotList=data.result.data
				let data1=await db.collection('opendb-news-articles').where({
					comment_status:1,
					article_status:1
				}).limit(5).get()
				console.log(data1)
				this.openComments=data1.result.data
			},
			enterNews(id){
				uni.navigateTo({
					url:'/pages/opendb-news-articles/detail?id='+id,
					events:{
						// 监听该事件
						refreshData:(data)=>{
							console.log()
						}
					}
				})
			},
			getaccess(){
				uniCloud.callFunction({
					name:'uni-id-cf',
					data:{
						action:'getAccessToken',
						params:{}
					},
					success: (e) => {
						console.log(e)
					}
				})
			},
			...mapMutations({
				setInfor:'user/login'
			}),
			async imgSecCheck(){
				uniCloud.callFunction({
					name:'uni-id-cf',
					data:{
						action:'imgSecCheck',
						params:{}
					},
					success: (e) => {
						console.log(e)
					}
				})
			},
			async idcard(){
				uniCloud.callFunction({
					name:'uni-id-cf',
					data:{
						action:'idCard',
						params:{
							imageurl:'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-62ec721a-a0ff-4726-a9f0-0e206b972095/210be149-a4d8-4762-aed2-2b21a5c16b3f.png'
						}
					},
					success: (e) => {
						console.log(e)
					}
				})
			},
			async getuserinfo(e){
				console.log(e)
				console.log('id',this.getInfo.uid)
				const {result}= await db.collection('uni-id-users').doc(this.getInfo.uid).get()
				console.log('aa')
				console.log(result)
				uniCloud.callFunction({
					name:'uni-id-cf',
					data:{
						action:'wxBizDataCrypt',
						params:{
							iv:e.detail.iv,
							encryptedData:e.detail.encryptedData,
							sessionKey:result.data[0].sessionKey
						}
					},
					success: (e) => {
						console.log('e',e)
						// this.setInfor(e)
					}
				})
				
			},
			async getbanner(){
				const res= await db.collection('opendb-banner').get()
				const {result}=res
				this.bannerList=result.data
			},
			async getimage(){
				const res=await db.collection('opendb-banner').get()
				console.log(res)
				const {result}=res
				this.imageValue=result.data[0].bannerfile
			},
			deletes(e){
				// console.log(e)
				// console.log(e.tempFile.fileID) //文件的id
				uniCloud.callFunction({
					name:"uni-id-cf",
					data:{
						action:'deleteFile',
						params:{
							fileId:[e.tempFile.fileID]
						}
					}
				}).then(res=>{
					console.log(res)
				})
			},
			change(e) {
				this.current = e.detail.current;
			},
			upload(){
				this.$refs.files.upload()
			},
			// 获取上传状态
			select(e){
				console.log('选择文件：',e)
			},
			// 获取上传进度
			progress(e){
				console.log('上传进度：',e)
			},
			// 上传成功
			success(e){
				console.log('上传成功',e)
				console.log(this.imageValue)
				var bannerfile={
					extname:this.imageValue[0].extname,
					name:this.imageValue[0].name,
					url:this.imageValue[0].url,
					fileID:this.imageValue[0].fileID
				}
				console.log(bannerfile)
				const db = uniCloud.database();
				// 将上传功能的文件存到数据库中
				db.collection('opendb-banner').add({
					bannerfile
				}).then(res=>{
					console.log(res)
				})
			},
			// 上传失败
			fail(e){
				console.log('上传失败：',e)
			}
		}
	}
</script>

<style>
	.Index {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>
